//引入axios
import axios from 'axios'
import Cookies from "js-cookie";
import qs from 'qs';
import {
    router
} from '../router/index.js'
import store from '../store'


axios.defaults.baseURL = ajaxUrl;

axios.defaults.withCredentials=true;
//超时请求
axios.defaults.timeout = 1000000;
//响应拦截器即异常处理
axios.interceptors.response.use(response => {
    return response
}, err => {
    store.commit('changeConect', false);
    if (err && err.response) {
        app.error(err.response.status);
    } else {
        err.message = "连接到服务器失败"
    }
    return Promise.reject(err.response);
});

const app = {

    //get请求
    get(url, param) {

        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url,
                params: param ,
                headers: {
                    "auth_code": localStorage.getItem("auth_code")
                }
            }).then(res => {
                this.error(res.data.code);
                resolve(res)
            })
        })
    },
    //post请求
    post(url, param) {
      // qs.stringify(param),
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data: qs.stringify(param, {arrayFormat: 'repeat'}),
                headers: {
                    "auth_code": localStorage.getItem("auth_code"),
                }
            }).then(res => {
                this.error(res.data.code);
                resolve(res);
            }).catch((res) => {

                this.error(res.status);
                reject(res)
                //console.log(res)
            })
        })
    },
    postReal(url, param) {

    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url,
        data: param,
        headers: {
          "auth_code": localStorage.getItem("auth_code"),
        }
      }).then(res => {
        this.error(res.data.code);
        resolve(res);
      }).catch((res) => {

        this.error(res.status);
        //console.log(res)
      })
    })
  },
    getFiles(url, params, fileName) {
        fileName = fileName || "";
        return new Promise((resolve, reject) => {
            axios.get(url, {
                headers: {
                    "auth_code": localStorage.getItem("auth_code")
                },
                params: params,
                responseType: 'blob',
            }).then(res => {
                var url = window.URL.createObjectURL(res.data);
                var link = document.createElement('a');
                link.style.display = 'none';
                link.href = url;
                link.setAttribute('download',fileName);
                document.body.appendChild(link);
                link.click();
                resolve(res);
            }).catch(err => {
                reject(err)
            })
        })
    },
    setHeader(authCode) {

        localStorage.removeItem("auth_code");
        localStorage.setItem("auth_code", authCode);
    },

    error(status) {

        switch (status) {
            //请求没有权限状态码
            case 2:
                router.replace({
                    path: '/error-403'
                });
                break;
                //请求未登录状态码
            case 3:
            case 403:
                Cookies.remove('userName');
                router.replace({
                    path: '/login'
                });
                break;
                //请求未找到
            case 404:
                router.replace({
                    path: '/error-404'
                });
                break;

        }

    }


}
export default app;
